package service;

import java.util.List;
import java.util.Map;

import base.Service;
import bean.Bank;

public class BankS extends Service
{
	 public int getCount(String columnName,String keyword)
	 {
		 String sql="select count(*) from bank where bisDel=0 ";
	     if(!"".equals(columnName)&&!"".equals(keyword))
		    sql+=" and "+columnName+" like '%"+keyword+"%'";
	     return jdbc.queryForInt(sql);
	 }
	 public List getPage(int amount,int pageSize,int pageNum,String columnName,String keyword)
	 {
		 String sql="select * " +
                   "from (select top "+(pageSize*pageNum>amount?(amount-pageSize*(pageNum-1)):pageSize)+" * " +
                   	  "from(select top "+pageSize*pageNum+" bnumber,bname,baccount,bremark "+
                              "from bank "+
                              "where bisDel=0 ";
        if(!"".equals(columnName)&&!"".equals(keyword))
                              sql+=" and "+columnName+" like '%"+keyword+"%' ";
                         sql+="order by bnumber asc)temp1 "+
                         "order by bnumber desc)temp2 "+
                   "order by bnumber asc";
        //System.out.println(sql);
        return jdbc.queryForList(sql);
	 }
	
	 public Map getBank(String number)
	 { 
		 String sql="select * from bank where bisDel=0 and bnumber='"+number+"'";
	     return jdbc.queryForMap(sql);	
	 }
	 public void save(Bank bank)
	 {
		  String sql="";
  	      if(0==bank.getId())
		  {
			   sql="insert into bank(bnumber,bname,baccount,bremark,bisdel) values("+
	                              "'"+bank.getNumber()+"',"+
	                              "'"+bank.getName()+"',"+
	                              "'"+bank.getAccount()+"',"+
	                              "'"+bank.getRemark()+"',0)";
	      }
		  else
		  {
			   sql="update bank set bnumber='"+bank.getNumber()+"',"+
			                         "bname='"+bank.getName()+"',"+
	                              "baccount='"+bank.getAccount()+"',"+
	                              "bremark='"+bank.getRemark()+"' "+
	                 " where bid="+bank.getId() ;
		  }
		  //System.out.println(sql);
		  jdbc.execute(sql);
	 }
	 public void delBank(String number)
	 {
		  String sql="update bank set bisDel=1 where bnumber='"+number+"'";
		  jdbc.execute(sql);	 
	 }
}
